package com.javaclimb.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javaclimb.entity.Advertisement;
import com.javaclimb.entity.User;
import com.javaclimb.mapper.UserMapper;
import com.javaclimb.request.UserReq;
import com.javaclimb.service.UserService;
import com.javaclimb.util.MapWrapperUtils;
import com.javaclimb.util.ReturnData;
import com.javaclimb.util.ReturnDataForLayui;
import com.javaclimb.util.jwt.JwtUtil;
import com.javaclimb.util.weixin.WxAvatarDownloadThread;
import com.javaclimb.util.weixin.WxLoginUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.ServletContext;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public ReturnData selectUserByCode(String code) {
        //根据code去微信查询openid
        ReturnData returnData = WxLoginUtils.jsCode2Session(code);
        if (returnData.getCode() != ReturnData.KEY_CODE_SUCCESS) {
            return returnData;
        }
        User newUser = (User) returnData.getData();
        //查询用户是否已经注册，如果没有注册进行注册操作
        User dbUser = userMapper.selectUserByOpenid(newUser.getOpenid());
        if (dbUser == null) {       //没注册呢
            newUser.setHaveGetUserInfo(User.NOT_GET_USERINFO);
            int count = userMapper.insertUser(newUser);
            if (count <= 0) {
                return ReturnData.fail("登录失败，请重试");
            }
            Long userId = userMapper.selectUserByOpenid(newUser.getOpenid()).getId();
            Map<String, Object> map = MapWrapperUtils.builder(MapWrapperUtils.KEY_USER_ID, userId);
            String token = JwtUtil.creatToken(map);
            return ReturnData.notGetUserInfo(token);
        } else if (User.NOT_GET_USERINFO.equals(dbUser.getHaveGetUserInfo())) {        //已经注册过但是尚未完善用户信息
            Long userId = dbUser.getId();
            Map<String, Object> map = MapWrapperUtils.builder(MapWrapperUtils.KEY_USER_ID, userId);
            String token = JwtUtil.creatToken(map);
            return ReturnData.notGetUserInfo(token);
        } else {         //已经注册并已经完善了用户信息
            Long userId = dbUser.getId();
            Map<String, Object> map = MapWrapperUtils.builder(MapWrapperUtils.KEY_USER_ID, userId);
            String token = JwtUtil.creatToken(map);
            return (ReturnData) ReturnData.success().put(ReturnData.KEY_DATA, token);
        }
    }

    /**
     * 更新用户信息的微信部分
     */
    @Override
    public ReturnData updateUserOfWxPart(UserReq req) {


        if (!StringUtils.isEmpty(req.getNickName())) {
            User user1 = userMapper.selectUserByName(req.getNickName());
            if (Objects.isNull(user1)) {
                //保存
                User user = new User();
                BeanUtils.copyProperties(req, user);
                userMapper.insertUser(user);
                return ReturnData.success("保存成功");
            }

        }
        return ReturnData.success("授权成功");
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    @Override
    public ReturnDataForLayui getList(User user) {
        PageHelper.startPage(user.getPage(), user.getLimit());
        List<User> list = userMapper.getList(user);
        PageInfo<User> info = new PageInfo<>(list);
        return ReturnDataForLayui.success(list, info.getTotal());
    }
}
















